﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DaZhong.Test
{
    public class ApiResult
    {
        public string IP { get; set; }
        public string Port { get; set; }
        public int Year { get; set; }
        public int Month { get; set; }
        public int Day { get; set; }
        public int Hour { get; set; }
        public int Minute { get; set; }

        public ApiResult(string ip, string port)
        {
            IP = ip ?? "";
            Port = port ?? "";
            Year = DateTime.Now.Year;
            Month = DateTime.Now.Month;
            Day = DateTime.Now.Day;
            Hour = DateTime.Now.Hour;
            Minute = DateTime.Now.Minute;
        }
        /// <summary>
        /// 生成规则请查看文档
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            var sb = new StringBuilder(31);
            sb.Append("+IP");
            var arr = IP.Split('.');
            int sum1 = 0;
            int sum2 = 0;
            foreach (string t1 in arr)
            {
                var slot = Convert.ToInt32(t1).ToString("X").PadLeft(2, '0');
                sum1 = sum1 ^ int.Parse(slot, NumberStyles.HexNumber);
                sb.Append(slot);
            }
            var p = Convert.ToInt32(Port).ToString("X").PadLeft(4, '0');
            sum1 = sum1 ^ int.Parse(p.Substring(0, 2), NumberStyles.HexNumber);
            sum1 = sum1 ^ int.Parse(p.Substring(2, 2), NumberStyles.HexNumber);
            sb.Append(p);
            sb.Append(sum1.ToString("X").PadLeft(2, '0'));
            var y = Year.ToString("X").PadLeft(2, '0');
            sb.Append(y);
            var m = Month.ToString("X").PadLeft(2, '0');
            sb.Append(m);
            var d = Day.ToString("X").PadLeft(2, '0');
            sb.Append(d);
            var h = Hour.ToString("X").PadLeft(2, '0');
            sb.Append(h);
            var mi = Minute.ToString("X").PadLeft(2, '0');
            sb.Append(mi);
            sum2 = int.Parse(y, NumberStyles.HexNumber)
                      ^ int.Parse(m, NumberStyles.HexNumber)
                      ^ int.Parse(d, NumberStyles.HexNumber)
                      ^ int.Parse(h, NumberStyles.HexNumber)
                      ^ int.Parse(mi, NumberStyles.HexNumber);
            sb.Append(sum2.ToString("X").PadLeft(2, '0'));
            sb.Append("OK");
            return sb.ToString();
        }

        
    }
    
    
}
